<?php
/**
 * 获取设备状态接口
 *
 * 
 */
 
use Workerman\Worker;
use Workerman\Mysql;

 class mod_settings_user{
	
	public function entry($param){
		
		return $this->do_mod_settings_user($param);
	}

	
	/** 
	* do_mod_settings_user  
	* 获取批次等级分布
	* 
	* @access private 
	* @param $param batch
	* @since 1.0 
	* @return array(errcode,errmsg,rtv) 
	*/
	private function do_mod_settings_user($param){
		$action = $param["action"];
        if($action=='password'){
            $id = $param['id'];
            $password = $param['password'];
            
            if(!empty($id)){
                if(!empty($password)){
                    sql_update("hf_mes_user",array('pwd'=>strtoupper(md5($password))), array('id'=>$id));
                    $obj = 'done';
                }
            }
        }

        if($action=='check_user'){
            $id = $param['id'];
            $data = $param['data'];
            $obj = checkUniqueValue_jsValidate("hf_mes_user", $id, $data, "user");

        }

        if($action=='check_code'){
            $id = $param['id'];
            $data = $param['data'];
            $obj = checkUniqueValue_jsValidate("hf_mes_user", $id, $data, "code");

        }
        
        /*获取user信息*/
        if(empty($action) || $action==""){
            
            $sql = "SELECT a.id, a.code, a.name, a.user, b.name,c.name, if( a.stat=1, '开启', '关闭') as stat, 
                        a.mobile, a.tel, a.addr, a.email
                        FROM hf_mes_user a
                        INNER JOIN hf_mes_department b ON b.id = a.department
                        left JOIN hf_mes_groups c ON a.groups= c.id
                        ";
                        
                
            $dp=sql_queryIdx($sql);
            //print_r($dp);
            //$obj = "{\"data\": " . json_encode($dp) . "}";
            $obj = new stdClass();
            $obj-> data = $dp;
            
        }

        /*获取group与department信息*/
        if($action == "get_groups"){
            $sql = "select id, name from hf_mes_groups;";
            $dp=sql_query($sql);
            // var_dump(000,  $dp) ;
            //$obj = "{\"data\": " . json_encode($dp) . "}" ;
            $obj = new stdClass();
            $obj-> data = $dp;
            
        }

        if($action == "get_department"){
            $sql = "select id, name from hf_mes_department;";
            $dp=sql_query($sql);
            //print_r($dp);
            //$obj = "{\"data\": " . json_encode($dp) . "}" ;
            $obj = new stdClass();
            $obj-> data = $dp;
        }


        /*添加新user*/
        if($action == "add"){
            $name = $param["name"];
            $code = $param["code"];
            $user = $param["user"];
            $stat = $param["stat"];
            $department = $param["department"];
            $groups = $param["groups"];
            $mobile = $param["mobile"];
            $tel = $param["tel"];
            $addr = $param["addr"];
            $email = $param["email"];
            $note = $param["note"];
            
            $a = sql_insert("hf_mes_user",array('name'=>$name,'code'=>$code,'user'=>$user,'stat'=>$stat,
                                                                'department'=>$department,'groups'=>$groups,'mobile'=>$mobile,'tel'=>$tel,'addr'=>$addr,'email'=>$email,'note'=>$note));
            $obj = "done";
        }

        /*编辑user*/
        if($action == "edit"){
            $id = $param["id"];
            $name = $param["name"];
            $code = $param["code"];
            $user = $param["user"];
            $stat = $param["stat"];
            $department = $param["department"];
            $groups = $param["groups"];
            $mobile = $param["mobile"];
            $tel = $param["tel"];
            $addr = $param["addr"];
            $email = $param["email"];
            $note = $param["note"];
            
            sql_update("hf_mes_user",array('name'=>$name,'code'=>$code,'user'=>$user,'stat'=>$stat,
                                                                'department'=>$department,'groups'=>$groups,'mobile'=>$mobile,'tel'=>$tel,'addr'=>$addr,'email'=>$email,'note'=>$note),array('id'=>$id));
         
            $obj = "done";
        }

        /*删除user*/
        if($action == "delete"){
            $id = $param["id"];
            sql_delete("hf_mes_user", array('id'=>$id));
            $obj = "done";
        }
		return array("errcode"=> 0,"errmsg"=>"","rtval"=>$obj);
	}
	 
	/** 
	* create  
	* 通过 call_user_func 获得本class的实例
	* 
	* @access public 
	* @since 1.0 
	* @return object 本类的实例
	*/	
    public static function create(){  
        static $instance;
        if (!$instance){  
			$instance = new mod_settings_user();  
        }
		return $instance;  
    }   
 }
 

?>